package com.elinkway.infinitemovies.view;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.AttributeSet;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.le123.ysdq.R;

public class MyLittleDots extends HorizontalScrollView {
	
	private LinearLayout dotsContainer;
	private LinearLayout.LayoutParams defaultLayoutParams;
	private ViewPager pager;
	private int currentPosition = 0;
	private int dotsCount=0;
	private OnPageChangeListener pageListener;
	
	public MyLittleDots(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
		setFillViewport(true);
		setWillNotDraw(false);

		dotsContainer = new LinearLayout(context);
		dotsContainer.setOrientation(LinearLayout.HORIZONTAL);
		dotsContainer.setLayoutParams(new LayoutParams(
				LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
		addView(dotsContainer);
		defaultLayoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

		pageListener = new PageListener();
	}

	public MyLittleDots(Context context, AttributeSet attrs) {
		this(context,attrs, 0);
		// TODO Auto-generated constructor stub
	}
	public MyLittleDots(Context context) {
		this(context,null);

		
	}
	public void setViewPager(ViewPager pager, int count) {
		this.pager = pager;
		dotsCount=count;
		if (pager.getAdapter() == null) {
			throw new IllegalStateException(
					"ViewPager does not have adapter instance.");
		}

		pager.setOnPageChangeListener(pageListener);
		notifyDataSetChanged();
	}
	public int getCount(){
		return dotsCount;
	}
	public void notifyDataSetChanged() {

		dotsContainer.removeAllViews();

//		dotsCount = pager.getAdapter().getCount();

		for (int i = 0; i < dotsCount; i++) {

				addDot(i);

		}
	}
	private void addDot(final int position) {

		ImageView dot = new ImageView(getContext());
		dot.setImageResource(R.drawable.gallery_dot_selector);
		dot.setPadding(4, 0, 0, 0);
		if(position==0){
			dot.setSelected(true);
		}
		dotsContainer.addView(dot,position,defaultLayoutParams);
	}
	private class PageListener implements OnPageChangeListener {


		@Override
		public void onPageSelected(int position) {
			if(position>=dotsCount){
				position = position%dotsCount;
			}
			if(position<0){
				position=-position;
			}
			setCurrentPosition(position);
		}

		@Override
		public void onPageScrollStateChanged(int arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {
			// TODO Auto-generated method stub
			
		}
	}
	private void setCurrentPosition(int position) {
		// TODO Auto-generated method stub
		
		dotsContainer.getChildAt(currentPosition).setSelected(false);
		dotsContainer.getChildAt(position).setSelected(true);
		currentPosition=position;
	}
}
